
package org.privat24.api.misc;

import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 *
 * @author dezmond
 */
public class TagReader {
    
    public static String getFieldValue(String fieldName, String xmlString) {
        
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder; 
            try {
                builder = factory.newDocumentBuilder();

                InputSource source = new InputSource();
                source.setCharacterStream(new StringReader(xmlString));
                try {
                    Document doc = builder.parse(source);
                    Node node = doc.getElementsByTagName(fieldName).item(0);
                    if(node==null){
                        return null;
                    } else {
                        return node.getTextContent();
                    }
                } catch (IOException ex) {
                    return null;
                } catch (DOMException ex) {
                    return null;
                } catch (SAXException ex) {
                    return null;
                }
            } catch (ParserConfigurationException ex) {
                return null;
            }

    }

}
